package mta.localization;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Dmitry Nikolaev.
 * User: dima
 * Date: 19.01.2009
 * Time: 18:40:56
 */
public class LocalizationPackage {

    HashMap<ModulePhrase, String> moduleTranslations = new HashMap<ModulePhrase, String>();
    HashMap<String, String> translations = new HashMap<String, String>();

    public Set<ModulePhrase> getAllModulePhrases() {
        return new HashSet<ModulePhrase>(moduleTranslations.keySet());
    }

    public Set<ModulePhrase> getUntranslatedModulePhrases() {
        Set<ModulePhrase> result = new HashSet<ModulePhrase>();
        for (ModulePhrase mphrase : moduleTranslations.keySet()) {
            if (moduleTranslations.get(mphrase) == null) {
                result.add(mphrase);
            }
        }
        return result;
    }

    public Set<String> getUntranslatedPhrases() {
        Set<String> result = new HashSet<String>();

        for (ModulePhrase mPhrase : getUntranslatedModulePhrases()) {
            String phrase = mPhrase.getPhrase();
            if (!hasTranslation(phrase)) {
                result.add(phrase);
            }
        }

        return result;
    }

    public Set<String> getUntranslatedPhrasesIncludePartial() {
        Set<String> result = new HashSet<String>();

        for (ModulePhrase mPhrase : getUntranslatedModulePhrases()) {
            String phrase = mPhrase.getPhrase();
            result.add(phrase);
        }

        return result;
    }

    public boolean hasModulePhrase(ModulePhrase phrase) {
        return moduleTranslations.containsKey(phrase);
    }

    public boolean hasTranslationForModulePhrase(ModulePhrase untranslatedPhrase) {
        if (hasModulePhrase(untranslatedPhrase)) {
            if (moduleTranslations.get(untranslatedPhrase) != null) {
                return true;
            }
        }
        return false;
    }

    public String getTranslationForModulePhrase(ModulePhrase untranslatedPhrase) {
        if (!hasTranslationForModulePhrase(untranslatedPhrase)) {
            throw new IllegalArgumentException("There is no translation for '" + untranslatedPhrase + "'");
        }
        return moduleTranslations.get(untranslatedPhrase);
    }

    public void setTranslationForModulePhrase(ModulePhrase untranslatedPhrase, String translation) {
        moduleTranslations.put(untranslatedPhrase, translation);

        if (translation != null) {
            translations.put(untranslatedPhrase.getPhrase(), translation);
        }
    }

    public void setTranslation(String module, String original, String translation) {
        setTranslationForModulePhrase(new ModulePhrase(module, original), translation);
    }

    public void setTranslationForAllModules(String original, String translation) {
        if (translations.containsKey(original)) {
            for (ModulePhrase modulePhrase : moduleTranslations.keySet()) {
                if (modulePhrase.getPhrase().equals(original)) {
                    moduleTranslations.put(modulePhrase, translation);
                }
            }
            translations.put(original, translation);
        } else {
            throw new IllegalArgumentException("There is no modules with phrase '" + original + "'");
        }
    }

    public boolean hasTranslation(String module, String original) {
        return hasTranslationForModulePhrase(new ModulePhrase(module, original));
    }

    public boolean hasTranslation(String original) {
        if (translations.get(original) != null) return true;
        else return false;
    }

    public String getTranslation(String original) {
        if (!hasTranslation(original)) {
            throw new IllegalArgumentException("There is no translation for phrase '" + original + "'");
        }
        return translations.get(original);
    }

    public Set<String> getAllModules() {
        //todo: optimize
        Set<String> result = new HashSet<String>();

        for (ModulePhrase phrase : this.moduleTranslations.keySet()) {
            if (!result.contains(phrase.getModule())) {
                result.add(phrase.getModule());
            }
        }
        return result;
    }

    public HashMap<String, String> getModulePhrases(String moduleName) {
        HashMap<String, String> result = new HashMap<String, String>();

        for (ModulePhrase phrase : this.moduleTranslations.keySet()) {
            if (moduleName.equals(phrase.getModule())) {
                result.put(phrase.getPhrase(), hasTranslationForModulePhrase(phrase) ?
                        getTranslationForModulePhrase(phrase) : null);
            }
        }

        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof LocalizationPackage)) return false;

        LocalizationPackage that = (LocalizationPackage) o;

        return moduleTranslations.equals(that.moduleTranslations) && translations.equals(that.translations);

    }

    @Override
    public int hashCode() {
        int result = moduleTranslations.hashCode();
        result = 31 * result + translations.hashCode();
        return result;
    }
}
